package cn.edu.scau.cmi.liangzaoqing.client;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.liangzaoqing.hibernate.domain.Major;
import cn.edu.scau.cmi.liangzaoqing.util.HibernateSessionFactoryUtil;

/*
 * 在上一个例子的基础上，SessionFactory单例的构建，从而避免资源的浪费和潜在的不一致情况的出现
 * 
 * */

public class HibernateClient5HibernateByHibernateFactory {

	public static void main(String[] args) {
		Major major=new Major();
		major.setName("车辆工程");
		major.setId(5L);
		
		newMajor(major);
	}

		public static void newMajor(Major major) {
			HibernateSessionFactoryUtil hibernateSessionFactoryUtil= new HibernateSessionFactoryUtil();
			Session session = hibernateSessionFactoryUtil.getSession();
			Transaction transaction = session.beginTransaction();
//	 		(1) 直接使用Hibernate的CRUD功能		
			session.save(major);

			transaction.commit();
			session.close();
		}
}